/**
 * 
 */
package org.picWeb.util.preferences;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Date;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import lombok.Cleanup;

/**
 * @author zjm
 * @version 1.0 org.picWeb.util.preferences/SystemGlobal.java 2011-7-19
 */
public class SystemGlobal implements VariableStore {
	public static final String APP_PATH = "app.path";
	public static final String CONFIG_FILE = "config.file";

	private static SystemGlobal global;
	private String proFile;

	private Properties pro = new SortedProperties();

	private Variable expander = new Variable(this, "${", "}");

	private SystemGlobal() {
	}
	
	public String getVariableValue(String variableKey) {
		String expansion = pro.getProperty(variableKey);
		return expander.expandVariables(expansion);
	}

	public static void initGlobal(String appPath, String filePath) {
		if (null == global) {
			global = new SystemGlobal();
		}
		global.buildGlobalPro(appPath, filePath);
	}

	public static void loadPro() {
		try {
			@Cleanup
			InputStream is = new BufferedInputStream(new FileInputStream(global.proFile));
			global.pro.load(is);
			global.expander.clearCache();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void savePro() {
		try {
			@Cleanup
			OutputStream os = new BufferedOutputStream(new FileOutputStream(global.proFile));
			
			global.pro.store(os, "propeties store at " + new Date(System.currentTimeMillis()));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void setValue(String key, String value){
		global.pro.setProperty(key, value);
		global.expander.clearCache();
	}

	public static String getValue(String key) {
		return global.getVariableValue(key);
	}

	public static boolean getBoolean(String key) {
		return "true".equals(getValue(key));
	}

	public static int getInt(String key) {
		String value = getValue(key);

		if (null == value || "".equals(value)) {
			return 0;
		}
		return Integer.valueOf(value);
	}

	public static Properties getPro(){
		return global.pro;
	}

	private void buildGlobalPro(String appPath, String filePath) {
		proFile = appPath.lastIndexOf("/") == appPath.length() ? appPath + filePath : appPath + "/" + filePath;

		pro.setProperty(APP_PATH, appPath);
		pro.setProperty(CONFIG_FILE, proFile);

		loadPro();
	}

	public static void main(String[] args) {
		System.out.println(1);
	}
}

class SortedProperties extends Properties {
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public synchronized Enumeration keys() {
		Enumeration keysEnum = super.keys();
		Vector keyList = new Vector();
		while (keysEnum.hasMoreElements()) {
			keyList.add(keysEnum.nextElement());
		}
		Collections.sort(keyList);
		return keyList.elements();
	}
}
